- many_thirds = affair.deals.map(&:deal_third).uniq.count > 1
- if affair
  - affairs_controller = affair.class.name.tableize
  - deal_class = affair.gap_class || affair.class.deal_class
  .affair
    .deals.active-list
      %table.list
        %thead
          %tr
            %th.act
              %i.icon.icon-unlink
            %th= deal_class.human_attribute_name(:done_at)
            %th= deal_class.human_attribute_name(:deal_type)
            - if many_thirds
              %th= deal_class.human_attribute_name(:third)
            %th= deal_class.human_attribute_name(:reference)
            %th= deal_class.human_attribute_name(:reference_number)
            %th= deal_class.human_attribute_name(:amount)
        %tbody
          - affair.deals.sort_by{ |a| a.dealt_at || a.created_at }.each do |deal|
            %tr
              %td.act
                - if !current_deal || (deal != current_deal && deal.detachable?)
                  - if affair.gaps.none?
                    - if deal.is_a? Regularization
                      = link_to({ action: :destroy, controller: :regularizations, id: deal.id, redirect: request.fullpath }, method: :delete, data: { confirm: :are_you_sure.tl }, class: "remove") do
                        %i
                        = :destroy.ta
                    - if deal.is_a? DebtTransfer
                      = link_to({ action: :destroy, controller: :debt_transfers, id: deal.id, redirect: request.fullpath }, method: :delete, data: { confirm: :are_you_sure.tl }, class: "remove") do
                        %i
                        = :destroy.ta
                    - else
                      = link_to({action: :detach, controller: affairs_controller, id: deal.affair_id, :deal_type => deal.class.name.underscore, :deal_id => deal.id, redirect: request.fullpath}, method: :delete, data: {confirm: :are_you_sure.tl}, class: "unlink") do
                        %i
                        = :detach.ta
                  - if deal.is_a? Gap
                    = link_to({action: :detach_gaps, controller: affairs_controller, id: deal.affair_id, redirect: request.fullpath}, method: :delete, data: {confirm: :are_you_sure.tl}, class: "remove") do
                      %i
                      = :destroy.ta
              %td.dat.dealt-on= deal.dealt_at.to_date.l
              %td.type
                = deal.class.model_name.human
                - if deal.is_a? DebtTransfer
                  = " (#{deal.sale_regularization? ? Purchase.model_name.human : Sale.model_name.human})"
              - if many_thirds
                %td.third= link_to(deal.deal_third.full_name, send("backend_#{deal.deal_third.class.name.underscore}_path", deal.deal_third))
              %td.name
                - if deal.is_a? DebtTransfer
                  - foreign_deal = deal.debt_transfer_affair
                  = (foreign_deal.deals.reject{ |d| d.is_a?(DebtTransfer) }.map { |d| link_to(d.number, controller: "/backend/" + d.class.name.tableize, action: :show, id: d.id) }.to_sentence).html_safe

                - else
                  = link_to(deal.number, controller: "/backend/" + deal.class.name.tableize, action: :show, id: deal.id)
              %td= deal.has_attribute?(:reference_number) ? deal.reference_number : ""
              %td.amount{class: (deal.good_deal? ? "debit" : "credit")}
                %span.value= deal.our_deal_balance.l(currency: deal.currency) if deal.journal_entry

    %table.list
      %tbody
        %tr.total
          - if !affair.closed? || (affair.closed? && affair.credit.zero?)
            %th.txt
              - types.each_with_index do |type, index|
                - model = type.underscore
                - klass = type.classify.constantize
                = dropdown_menu_button "add_#{model}".to_sym, icon: false, class: (index.zero? ? 'primary' : nil), dropup: true do |l|
                  - excluded = affair.deals_of_type(klass).pluck(:id) + klass.where.not(currency: affair.currency).pluck(:id)
                  - existings = klass.where(klass.deal_third.name => affair.third).affairable.where.not(id: excluded)
                  - if existings.any?
                    - existings.each do |existing|
                      - l.item existing.label, { controller: affairs_controller, action: :attach, id: affair.id, deal_id: existing.id, deal_type: model, redirect: request.fullpath }, method: :post
                    - l.separator
                  - l.item :new.tl, { controller: model.pluralize, action: :new, klass.deal_third.foreign_key => affair.third.id, amount: affair.balance.abs, redirect: request.fullpath, affair_id: affair.id }.merge(new_record_options[model.to_s.to_sym] || {})

              - if affair.debt_transferable?
                - model = affair.is_a?(SaleAffair) ? 'purchase_invoice' : 'sale'
                - klass = model.classify.constantize
                - excluded = affair.deals_of_type(klass).pluck(:id) + klass.where.not(currency: affair.currency).pluck(:id)
                - existings = klass.where(klass.deal_third.name => affair.third).affairable.where.not(id: excluded)
                - if existings.any?
                  .btn-group
                    = dropdown_menu_button "add_#{model}".to_sym, icon: false, dropup: true, force_menu: true do |l|
                      - existings.each do |existing|
                        - l.item existing.label, { controller: :debt_transfers, action: :create, id: affair.id, deal_affair_id: existing.affair.id, redirect: request.fullpath }, method: :post
                      -# l.separator
                    -# l.item :new.tl, { controller: model.pluralize, action: :new, klass.deal_third.foreign_key => affair.third.id, amount: affair.balance.abs, redirect: request.fullpath, affair_id: affair.id }

              - if affair.finishable?
                .btn-group
                  - if affair.regularizations.empty?
                    = link_to("convert_balance_into_#{affair.losing? ? :loss : :profit}".tl, {controller: affairs_controller, action: :finish, id: affair.id, redirect: request.fullpath}, method: :post, class: "btn")
                  = dropdown_menu_button :add_regularization.tl, icon: false, dropup: true do |l|
                    - existings = JournalEntry.where(id: affair.third_account.journal_entry_items.where(letter: nil).select(:entry_id)).where(resource_id: nil)
                    - if existings.any?
                      - existings.each do |existing|
                        - amount = existing.items.where(account: affair.third_account).sum('real_debit - real_credit')
                        - displayed_label = existing.number
                        - displayed_label << " - " + amount.l(currency: existing.real_currency)
                        - displayed_label << " - " + existing.items.map(&:name).join(', ')

                        - l.item displayed_label, { controller: :regularizations, action: :create, affair_id: affair.id, journal_entry_id: existing.id, redirect: request.fullpath }, method: :post
                      - l.separator
                    - l.item :new.tl, { controller: :journal_entries, action: :new, affair_id: affair.id, nature: :various, currency: affair.currency, redirect: request.fullpath }
          %th= :balance.tl
          %td.total.amount.sum{class: (affair.balance.zero? ? "debit" : "credit")}
            = affair.balance.l(currency: affair.currency)
    - if affair.balanced? && !affair.letterable?
      %p.inline-message.warning
        %i.icon.icon-warning
        - if affair.multi_thirds?
          = :the_automatic_lettering_cannot_be_done_in_accountancy_because_too_many_thirds_are_present.tn
        - else
          = :the_automatic_lettering_cannot_be_done_in_accountancy.tn
